package fr.upmc.i3s.upmcontact.views.dialogs;

import fr.upmc.i3s.upmcontact.R;
import fr.upmc.i3s.upmcontact.backend.BackEndBridgeImpl;
import fr.upmc.i3s.upmcontact.contents.LostFoundObject;
import fr.upmc.i3s.upmcontact.tools.BitmapFactoryAdapter;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class ObjectDetailsDialog extends Activity {
	
	private static final String TAG = "ObjectDetailsDialog";
	
	public static String PARAM_OBJECT = "ObjectDetailsDialog.object";
	public static String PARAM_NEW_STATUS = "ObjectDetailsDialog.new.status";
	
	private LostFoundObject lfo;
	private int activityWidth;

	private TextView tvName;
	private ImageView ivPhoto;
	private TextView tvDescription;
	private CheckBox cbChangeStatus;
	private Button bChangeStatus;
	private Button bContact;
	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_dialog_object);
        
        // set dialog size (80% screen width) :
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        this.activityWidth = (int) (metrics.widthPixels * 0.80);
        getWindow().setLayout(activityWidth, LayoutParams.WRAP_CONTENT);
        
        // get extras parameters 
        this.lfo = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
        	this.lfo = (LostFoundObject) extras.getSerializable(PARAM_OBJECT);
        }
        Log.d(TAG, "extras.LostFoundObject = "+lfo);
        
        fetchViews();
        customizeViews();
        setListeners();
	}
	
	private void fetchViews() {
		this.tvName = (TextView) this.findViewById(R.id.textView_dialog_name);
		this.ivPhoto = (ImageView) this.findViewById(R.id.imageView_dialog_photo);
		this.tvDescription = (TextView) this.findViewById(R.id.textView_dialog_description);
		this.cbChangeStatus = (CheckBox) this.findViewById(R.id.checkbox_dialog_change_status);
		this.bChangeStatus = (Button) this.findViewById(R.id.button_dialog_change_status);
		this.bContact = (Button) this.findViewById(R.id.button_dialog_contact);
	}
	
	private void customizeViews() {
		if(lfo != null) {
			this.tvName.setText(lfo.getName());
	 		if(lfo.getPhotoPath() != null) { 
	 			Bitmap bm = BitmapFactoryAdapter.decodeFile(lfo.getPhotoPath(), 400);
	 			ivPhoto.setImageBitmap(bm);
	 		}
	 		this.ivPhoto.setAdjustViewBounds(true);
			this.tvDescription.setText(Html.fromHtml(lfo.getDescription()+"<br />"+
										"<b>"+getString(R.string.prompt_object_lost_place)+" : "
										+lfo.getPlace()+"</b>")); 
			if(lfo.getType() == BackEndBridgeImpl.PARSE_OBJECT_LOST_VALUE) {
				this.bChangeStatus.setText(R.string.action_lost_change_status);
			} else if(lfo.getType() == BackEndBridgeImpl.PARSE_OBJECT_FOUND_VALUE) {
				this.bChangeStatus.setText(R.string.action_found_change_status);
			}
		}
	}
	
	private void setListeners() {
		this.bContact.setOnClickListener(onContactClickListener);
		this.cbChangeStatus.setOnCheckedChangeListener(onStatusCheckedChangeListener);
		this.bChangeStatus.setOnClickListener(onChangeStatusClickListener);
	}
	
	
	///////////////
	// LISTENERS //
	///////////////
	
	private OnClickListener onContactClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("mailto",lfo.getSignaledBy().getEmail(), null));
			emailIntent.putExtra(Intent.EXTRA_SUBJECT, "UPMContact-About:"+lfo.getName());
			startActivity(Intent.createChooser(emailIntent, getString(R.string.email_to_user)));
		}
	};
	
	private OnClickListener onChangeStatusClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			intent.putExtra(PARAM_OBJECT, lfo);
			if(lfo.getStatus() == BackEndBridgeImpl.PARSE_OBJECT_STATUS_SIGNALED) {
				intent.putExtra(PARAM_NEW_STATUS, BackEndBridgeImpl.PARSE_OBJECT_STATUS_ENDED);
			} else {
				intent.putExtra(PARAM_NEW_STATUS, BackEndBridgeImpl.PARSE_OBJECT_STATUS_SIGNALED);
			}
			setResult(RESULT_OK, intent);
			finish();
		}
	};
	
	private OnCheckedChangeListener onStatusCheckedChangeListener = new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(CompoundButton cb, boolean value) {
			if(value == true) {
				bChangeStatus.setVisibility(View.VISIBLE);
			} else {
				bChangeStatus.setVisibility(View.GONE);
			}
		}
	};

}
